'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');

var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _autoComplete = require('antd/lib/auto-complete');

var _autoComplete2 = _interopRequireDefault(_autoComplete);

var _input = require('antd/lib/input');

var _input2 = _interopRequireDefault(_input);

var _utils = require('../../utils');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var AutoComplete = function (_React$Component) {
  (0, _inherits3['default'])(AutoComplete, _React$Component);
  (0, _createClass3['default'])(AutoComplete, null, [{
    key: 'getDerivedStateFromProps',
    value: function getDerivedStateFromProps(props, _ref) {
      var prevProps = _ref.prevProps;

      var nextState = {
        prevProps: props
      };
      if (prevProps && prevProps.value !== props.value) {
        nextState.value = props.value;
      }
      return nextState;
    }
  }]);

  function AutoComplete(props) {
    (0, _classCallCheck3['default'])(this, AutoComplete);

    var _this = (0, _possibleConstructorReturn3['default'])(this, (AutoComplete.__proto__ || Object.getPrototypeOf(AutoComplete)).call(this, props));

    _this.onSearch = function (value) {
      if (_this.props.onSearch) {
        _this.props.onSearch();
      }
      _this.setState({
        dataSource: value && !value.match(/[a-z|%]/gi) ? _this.props.dataSource.map(function (key) {
          return { value: '' + value + key };
        }) : []
      });
    };

    _this.onChangeEnd = function (e) {
      var target = e.target;

      var v = target ? target.value : e;
      if (v !== _this.props.value) {
        var o = {
          showAll: false,
          value: v
        };
        _this.setState(o);
        _this.props.onChange(v);
      }
    };

    _this.onChange = function (value) {
      _this.setState({
        value: value
      });
    };

    _this.state = {
      dataSource: [],
      value: props.value
    };
    return _this;
  }

  (0, _createClass3['default'])(AutoComplete, [{
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props = this.props,
          onChange = _props.onChange,
          dataSource = _props.dataSource,
          size = _props.size,
          props = (0, _objectWithoutProperties3['default'])(_props, ['onChange', 'dataSource', 'size']);
      var _state = this.state,
          value = _state.value,
          options = _state.dataSource;

      return _react2['default'].createElement(
        _autoComplete2['default'],
        (0, _extends3['default'])({}, props, {
          value: value || undefined,
          options: options,
          onSearch: this.onSearch,
          onChange: this.onChange,
          onBlur: this.onChangeEnd,
          onSelect: this.onChangeEnd,
          placeholder: this.props.placeholder || '--',
          dropdownMatchSelectWidth: false,
          dropdownClassName: 'editor-list-dropdown',
          getPopupContainer: function getPopupContainer(node) {
            return (0, _utils.getParentNode)(node, 'editor-list');
          }
        }),
        _react2['default'].createElement(_input2['default'], {
          size: size,
          onPressEnter: function onPressEnter(e) {
            _this2.onChangeEnd(e.target.value);
          }
        })
      );
    }
  }]);
  return AutoComplete;
}(_react2['default'].Component);

AutoComplete.propTypes = {
  onSearch: _propTypes2['default'].func,
  dataSource: _propTypes2['default'].array,
  placeholder: _propTypes2['default'].string,
  size: _propTypes2['default'].string,
  onChange: _propTypes2['default'].func,
  value: _propTypes2['default'].string
};
AutoComplete.defaultProps = {
  dataSource: ['px', '%', 'rem', 'em', 'vw', 'vh'],
  size: 'small'
};
exports['default'] = AutoComplete;
module.exports = exports['default'];